package cn.crm.controller;

import cn.crm.pojo.User;
import cn.crm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    //1.显示登录页面
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    //2.执行登录操作
    @RequestMapping("/dologin")
    public String dologin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          HttpSession session, Model model){
        User user = userService.login(username,password);
        if(user!=null){
            session.setAttribute("LOGINUSER",user);
            return "redirect:/customer";   //重定向
        }else{
            model.addAttribute("msg","用户名不存在或密码错误！");
            return "login";
        }
    }

}
